#!/usr/bin/env bash

set -o xtrace
set -o errexit
set -o pipefail

action="$1"; shift		# up/down
name="$1"; shift		# network name
iptlabel="yunion-dummy-gateway: $name"

source "/etc/yunion/dummy-gateway/$name"
[ -n "$OVS_BRIDGE" ] || OVS_BRIDGE=br0
[ -n "$BOND" ] || BOND=bond-gw
[ -n "$VETH" ] || VETH=veth-gw
VLAN="${VLAN-2}"
[ -n "$NETWORK" ] || NETWORK=10.0.2.0/24
[ -n "$GATEWAY" ] || GATEWAY=10.0.2.1/24

up() {
	down

	ip link add dev "$BOND" type bond
	ip link add dev "${VETH}a" type veth peer name "${VETH}b"

	ip link set "${VETH}b" master "$BOND"
	ovs-vsctl --may-exist add-port "$OVS_BRIDGE" "$BOND"
	if [ -n "$VLAN" -a "$VLAN" -gt 1 ]; then
		ovs-vsctl set Port "$BOND" "tag=$VLAN"
	else
		ovs-vsctl clear Port "$BOND" tag
	fi

	ip addr add "$GATEWAY" dev "${VETH}a"
	ip link set "$BOND" up
	ip link set "${VETH}a" up
	ip link set "${VETH}b" up
	sysctl -w net.ipv4.ip_forward=1
	iptables -t filter -P FORWARD ACCEPT
	iptables -t nat -A POSTROUTING -o "$OVS_BRIDGE" -s "$NETWORK" -m comment --comment "$iptlabel" -j MASQUERADE
}

down_() {
	ip link del "$BOND"
	ip link del "${VETH}a"
	iptables-save -c | grep -v "$iptlabel" | iptables-restore -c
}

down() {
	down_ || true
}

"$action"
